package org.infodavid.common.ejb;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.PrePassivate;
import javax.ejb.Remote;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.Persister;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

import org.infodavid.common.ejb.components.GenericPropertyComponent;
import org.infodavid.common.impl.model.GroupImpl;
import org.infodavid.common.model.IGenericPropertiesMap;
import org.infodavid.common.model.IGenericProperty;
import org.infodavid.common.model.IUserGroup;
import org.infodavid.common.model.impl.persisters.VersionSafeSingleTableEntityPersister;
import org.infodavid.common.persistence.INamed;
import org.infodavid.common.persistence.UserGroupDataService;

/**
 * The Class UserGroupBean.
 */
@Entity(name = "IUserGroup")
@org.hibernate.annotations.Entity(dynamicUpdate = true, selectBeforeUpdate = true, dynamicInsert = true, optimisticLock = OptimisticLockType.VERSION)
@Persister(impl = VersionSafeSingleTableEntityPersister.class)
@Table(name = "USERGROUPS")
@NamedQueries(value = {
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_COUNT, query = "select count(d."
        + IUserGroup.KEY + ") from IUserGroup d"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND, query = "select d from IUserGroup d"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_ACTIVE_COUNT, query = "select count(d."
        + IUserGroup.KEY + ") from IUserGroup d where d.archived = 0"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_ACTIVE, query = "select d from IUserGroup d where d.archived = 0"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_BY_NAME, query = "select d from IUserGroup d where d.archived = 0 and :"
        + IUserGroup.NAME + " is not null and d." + IUserGroup.NAME + " = :" + IUserGroup.NAME),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_ACTIVE_BY_ROLE, query = "select d from IUserGroup d where d."
        + IUserGroup.ROLE + " = :" + IUserGroup.ROLE + " and d.archived = 0"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_ACTIVE_BY_ROLE_COUNT, query = "select count(d.key) from IUserGroup d where d."
        + IUserGroup.ROLE + " = :" + IUserGroup.ROLE + " and d.archived = 0"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_BY_GUEST, query = "select d from IUserGroup d where d.archived = 0 and d."
        + IUserGroup.GUEST + " = :" + IUserGroup.GUEST),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_BY_GUEST_COUNT, query = "select count(d.key) from IUserGroup d where d.archived = 0 and d."
        + IUserGroup.GUEST + " = :" + IUserGroup.GUEST),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_BY_CREATION_DATE, query = "select d from IUserGroup d where d.archived = 0 and d."
        + IUserGroup.CREATION_DATE + " between :date1 and :date2"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_BY_CREATION_DATE_COUNT, query = "select count(d."
        + IUserGroup.KEY
        + ") from IUserGroup d where d.archived = 0 and d."
        + IUserGroup.CREATION_DATE + " between :date1 and :date2"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_BY_MODIFICATION_DATE, query = "select d from IUserGroup d where d.archived = 0 and d."
        + IUserGroup.MODIFICATION_DATE + " between :date1 and :date2"),
    @NamedQuery(name = UserGroupDataService.QUERY_FIND_BY_MODIFICATION_DATE_COUNT, query = "select count(d."
        + IUserGroup.KEY
        + ") from IUserGroup d where d.archived = 0 and d."
        + IUserGroup.MODIFICATION_DATE + " between :date1 and :date2")
})
@Local(value = IUserGroup.class)
@Remote(value = IUserGroupRemote.class)
public class UserGroupBean extends GroupImpl {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1660345027625851664L;

	/**
	 * The Constructor.
	 * @throws Exception the exception
	 */
	protected UserGroupBean() throws Exception {
		super();
	}

	/**
	 * The Constructor.
	 * @param keyArg the key arg
	 * @throws Exception the exception
	 */
	protected UserGroupBean(final Long keyArg) throws Exception {
		super(keyArg);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.GroupImpl#init()
	 */
	@Override
	@PrePassivate
	public void init() throws Exception {
		super.init();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#getCreationDate()
	 */
	@Override
	@Column(name = CREATION_DATE, nullable = false, updatable = false)
	@Type(type = "org.infodavid.persistence.impl.hibernate.usertype.DateTimeUserType")
	@Index(name = "IDX_USERGROUPS_CREATIONDATE")
	public DateTime getCreationDate() {
		return super.getCreationDate();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#getModificationDate()
	 */
	@Override
	@Version
	@Column(name = MODIFICATION_DATE, nullable = false)
	@Type(type = "org.infodavid.persistence.impl.hibernate.usertype.DateTimeUserType")
	public DateTime getModificationDate() {
		return super.getModificationDate();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#getKey()
	 */
	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = ID, nullable = false)
	public Long getKey() {
		return super.getKey();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#isArchived()
	 */
	@Override
	@Column(name = ARCHIVED, nullable = false)
	@Index(name = "IDX_USERGROUPS_ARCHIVED")
	public boolean isArchived() {
		return super.isArchived();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.auth.model.impl.GroupImpl#getName()
	 */
	@Override
	@Column(name = NAME, length = 32, nullable = false, updatable = true)
	@Index(name = "IDX_USERGROUPS_NAME")
	public String getName() {
		return super.getName();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.auth.model.impl.GroupImpl#getComment()
	 */
	@Override
	@Column(name = COMMENT, length = 1024, nullable = true)
	public String getComment() {
		return super.getComment();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.auth.model.impl.GroupImpl#getProperties()
	 */
	@Override
	@Transient
	public IGenericPropertiesMap getProperties() {
		return super.getProperties();
	}

	/**
	 * Gets the data.
	 * @return the data
	 */
	@ElementCollection(fetch = FetchType.LAZY, targetClass = GenericPropertyComponent.class)
	@CollectionTable(name = "USERGROUPS_PROPERTIES", joinColumns = @JoinColumn(name = ID, nullable = false), uniqueConstraints = @UniqueConstraint(name = "UC_USERGROUPS_PROPERTIES", columnNames = {
	    INamed.NAME, ID
	}))
	@ForeignKey(name = "FK_USERGROUPS_PROPERTIES")
	protected List<IGenericProperty> getGenericData() {
		return super.getProperties().getData();
	}

	/**
	 * Sets the generic data.
	 * @param values the values
	 */
	protected void setGenericData(final List<IGenericProperty> values) {
		super.getProperties().setData(values);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.auth.model.impl.GroupImpl#isLocked()
	 */
	@Override
	@Column(name = LOCKED, nullable = false)
	public boolean isLocked() {
		return super.isLocked();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.GroupImpl#isGuest()
	 */
	@Override
	@Column(name = GUEST, nullable = false)
	@Index(name = "IDX_USERGROUPS_GUEST")
	public boolean isGuest() {
		return super.isGuest();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.GroupImpl#getRole()
	 */
	@Override
	@Enumerated(EnumType.STRING)
	@Column(name = ROLE, nullable = true, length = 32, updatable = true)
	@Index(name = "IDX_USERGROUPS_ROLE")
	public EUserRole getRole() {
		return super.getRole();
	}
}
